//
//  ModelScrollView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/10/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit



final class ModelsScroll: UIScrollView {
    
    // MARK: - Outlets
    
    lazy private var stackView : UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .leading
        stackView.distribution = .equalSpacing
        stackView.spacing = 12.0
        return stackView
    }()
    
    // MARK: - Props
    
    private var lastIndex: Int? = 0
    private var models: [Model] = []
    
    // MARK: - Init
    
    init (models: [Model]) {
        super.init(frame: .zero)
        self.models = models
        self.showsHorizontalScrollIndicator = false
        
        self.addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.top.equalToSuperview()
        }
        self.createScroll(models: models)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Methods
    
    @objc private func selectButton (sender: UIButton) {
        self.selectBtn(sender)
        if (lastIndex != nil) {
            self.deselectBtn(stackView.arrangedSubviews[lastIndex!] as! UIButton)
        }
        lastIndex = sender.tag
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setModel"),
                                        object: nil,
                                        userInfo: [ "model": models[sender.tag], "index": sender.tag ])
    }
    
    private func selectBtn (_ button: UIButton) {
        button.layer.borderWidth = 0.0
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .mediumDarkBlue
    }
    private func deselectBtn (_ button: UIButton) {
        button.layer.borderWidth = 1.0
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = .white
    }
    
    private func createScroll (models: [Model]) {
        for (i, model) in models.enumerated() {
            let btn = self.createItem(for: model)
            btn.tag = i
            stackView.addArrangedSubview(btn)
            btn.snp.makeConstraints { (make) in
                make.height.equalTo(48)
                make.centerY.equalToSuperview()
            }
        }
        self.selectBtn(self.stackView.arrangedSubviews[0] as! UIButton)
    }
    
    private func createItem (for model: Model) -> UIButton {
        let itemBtn = UIButton ()
        itemBtn.clipsToBounds = true
        itemBtn.layer.cornerRadius = 10
        itemBtn.layer.borderColor = UIColor.greyBlue.cgColor
        itemBtn.layer.borderWidth = 1.0
        itemBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 32)
        itemBtn.setTitleColor(.black, for: .normal)
        
        itemBtn.setTitle(model.name_ru, for: .normal)
        itemBtn.addTarget(self, action: #selector(selectButton(sender:)), for: .touchUpInside)
        return itemBtn
    }
}
